<?php
namespace app\sysAdmin\controller;
use app\common\Base;
use app\common\Compent;
use app\common\Emodel;
use think\Db;

class TeacherFunctionModule extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		if(request()->isPost()){
			$page = input('page');
			$rows = input('rows');
			$where["RESOURCE_RANGE"] = "teacher";
			$result = Db::table("T_SYS_RESOURCE")
			->where($where)
			->page($page, $rows)
			->order("PARENT_ID,WEIGHT")->select();
			$count = Db::table("T_SYS_RESOURCE")
					->where($where)
					->count();
			$data = selectResult($result);
			$list = [];
	        foreach ($data as $key => $obj) {
	            $obj["idsValue"]  = getPositionName($obj["IDS"]);
	            $list[$key] = $obj;
	        }
			$grid['rows'] = $list;
			$grid['total'] = $count;
			return $grid;
		}
	}

	public function addUpdate() {
		$all = request()->param();
		try {
			if(array_key_exists("IDS", $all)) {
				if($all["IDS"] == '') {
						$all["IDS"] = '0';
				}
				if(strpos(','.$all["IDS"],",0,") !== false) {
					$temp = ','.$all["IDS"];
					$all["IDS"] = str_replace(',0,','',$temp);
				}
			}
			if(array_key_exists("ID", $all)) {
				update('T_SYS_RESOURCE', $all);
			} else {
				insert('T_SYS_RESOURCE', $all);
			}
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	public function del() {
		$delIds = input("delIds");
		try {
			Db::name('T_SYS_RESOURCE')->where('ID','in',explode(",", $delIds))->delete();
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

}
